//
//  ComposeBtn.m
//  SinaWebo
//
//  Created by wwj on 16/1/10.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "ComposeBtn.h"
#import <POP.h>
#define kDuration 0.3

@implementation ComposeBtn

+(instancetype)composeBtn{
    return [[self alloc] init];
}

+(instancetype)composeBtnWithTitle:(NSString *)title image:(NSString *)img{
    ComposeBtn *btn = [ComposeBtn composeBtn];
    btn.title = title;
    btn.img = img;
    return btn;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    [self setTitle:title forState:UIControlStateNormal];
}

-(void)setImg:(NSString *)img{
    _img = img;
    [self setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}
-(void)setHighlighted:(BOOL)highlighted{}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)initView{
    [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.imageView.contentMode = UIViewContentModeCenter;
}

//-(void)show{
//    CGFloat height = kScreenH - self.frame.origin.y;
//    
//    CAKeyframeAnimation *trans = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
//    trans.values =@[@(height)];
//    trans.removedOnCompletion = NO;
//    trans.fillMode = kCAFillModeForwards;
//    [self.layer addAnimation:trans forKey:nil];
//
//    CAKeyframeAnimation *trans2 = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
//    trans2.duration = 0.5 * self.tag;
//    trans2.values =@[@(height), @0, @-4, @4, @-2, @2, @0];
//    trans2.removedOnCompletion = NO;
//    trans2.fillMode = kCAFillModeForwards;
//    [self.layer addAnimation:trans2 forKey:nil];
//}

-(void)show{
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    CGRect frame = self.frame;
    frame.origin.y = kScreenH;
    animation.fromValue = [NSValue valueWithCGRect:frame];
    animation.toValue = [NSValue valueWithCGRect:self.frame];
    animation.beginTime = CACurrentMediaTime() + 0.5 * self.tag;
    animation.springSpeed = 10.0;
    animation.springBounciness = 10.0;
    
    [self pop_addAnimation:animation forKey:nil];
    
}

-(void)hidden{
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    CGRect frame = self.frame;
    frame.origin.y = kScreenH;
    animation.toValue = [NSValue valueWithCGRect:frame];
    animation.beginTime = CACurrentMediaTime() + 0.05 * self.tag;
    [self pop_addAnimation:animation forKey:nil];
}

//-(void)hidden{
//    CGFloat height = kScreenH - self.frame.origin.y;
//    CGFloat duration = self.tag * 0.5;
//    [UIView animateWithDuration:duration animations:^{
//        CGRect frame = self.frame;
//        frame.origin.y = height;
//        self.frame = frame;
//    }];
//}


-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat tH = 20;
    CGFloat tY = contentRect.size.height - tH;
    CGFloat tW = contentRect.size.width;
    return CGRectMake(0, tY, tW, tH);
}

-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    return CGRectMake(0, 10, contentRect.size.width, contentRect.size.height - 20);
}

-(void)onClickBtnTouchDown{
    self.imageView.transform = CGAffineTransformMakeScale(1.1, 1.1);
}

-(void)onClickBtnDrag{
    self.imageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
}

-(CGPoint)pointWithTouch:(NSSet *)touches{
    UITouch *touch=[touches anyObject];
    return [touch locationInView:touch.view];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [self pointWithTouch:touches];
    if (CGRectContainsPoint(self.imageView.frame, point)) {
        [self onClickBtnTouchDown];
    }
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [self pointWithTouch:touches];
    if (CGRectContainsPoint(self.imageView.frame, point)) {
        [self onClickBtnDrag];
    }
}

-(void)onClickBtn{
    [UIView animateWithDuration:kDuration animations:^{
        self.imageView.transform = CGAffineTransformMakeScale(1.6, 1.6);
        self.titleLabel.transform = CGAffineTransformMakeScale(1.4, 1.4);
        self.imageView.alpha = 0;
        self.titleLabel.alpha = 0;
    }];
}

-(void)changeSize{
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *aplia = [CABasicAnimation animationWithKeyPath:@"opacity"];
    aplia.fromValue = @1.0;
    aplia.toValue = @0.0;
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.fromValue = @1.0;
    animation.toValue = @0.0;
   
    
    group.duration = kDuration;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.animations = @[aplia, animation];
    [self.layer addAnimation:group forKey:nil];
}


@end
